<template>
  <div class="topbar">
    <div class="warp topwarp">
      <div class="right">欢迎来到叩丁狼积分商城</div>
      <ul class="left">
        <li>
          <img :src="userInfo.headImg" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isShowLoginModal">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="loginbtn" @click="changeisShowModal(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { WeiXinLoginAPI } from "../request/api";
export default {
  data() {
    return {};
  },
  methods: {
    ...mapMutations({
      changeisShowModal: "showModal/changeisShowModal",
      changeIsShowLoginModal: "loginModal/changeIsShowLoginModal",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncchangeUserInfo: "userInfo/asyncchangeUserInfo",
    }),
  },
  computed: {
    ...mapState({
      isShowLoginModal: (state) => state.loginModal.isShowLoginModal,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  created() {
    this.$router.onReady(async () => {
      let paramscode = this.$route.query.code;
      if (paramscode) {
        let res = await WeiXinLoginAPI({
          code: paramscode,
        });
        if (res.cpde == 0) {
          alert("登陆成功");
          sessionStorage.setItem("x-auth-token", res["x-auth-token"]);
          this.changeIsShowLoginModal(true);
          this.$router.push(this.$route.path);
        } else if (res.cpde == 400) {
          //二维码失效
          alert("二位阿门失效，重新扫码");
          this.changeisShowModal(true);
        } else if (res.cpde == 407) {
          alert("请使用手机号再次登录");
          this.changeisShowModal(true);
          //存储uuid 用户手机号码登录需要用uuid来关联微信
          sessionStorage.setItem("uuid", res.uuid);
          this.$router.push(this.$route.path);
        }
      } else {
        let token = sessionStorage.getItem("x-auth-token");
        this.changeIsShowLoginModal(Boolean(token));
        if (token) {
          //获取用户信息   重新加载信息
          this.asyncchangeUserInfo();
        } else {
          //初始化数据
          this.initUserInfo();
        }
      }
    });
  },
  // watch: {
  //   "$route.path": {
  //     handler() {
  //       let token = sessionStorage.getItem("x-auth-token");
  //       this.changeIsShowLoginModal(Boolean(token));
  //     },
  //   },
  // },
};
</script>
 
<style lang = "less" scoped>
.topbar {
  height: 40px;
  background: #242b39;
  .topwarp {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    font-family: SourceHanSansSC;
    font-weight: 300;
    color: #fffefe;
    line-height: 40px;
    .left {
      li {
        float: left;
        padding: 0 10px;
        text-align: center;
        cursor: pointer;
        img {
          width: 26px;
          height: 26px;
          background: #7d8696;
          border: 1px solid #8891a1;
          border-radius: 13px;
          vertical-align: middle;
        }
        &.loginbtn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          padding: 0;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
    }
  }
}
</style>